import {
  indexApi
} from '../../api/index';
import {
  request
} from '../../utils/request';
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
Page({
  data: {
    defaultData: {
      title: "共享服务平台", // 导航栏标题
      // imageSrc: "/images/home.png"
    },
    isLogin: false,
    avatar: "https://zntg.swehszn.com/file/WechatIMG640.png",
    nickName: "用户名",
    withdraw: 0,
    coupon: 0,
    total: 0
  },

  onShow() {
    this.checkToken()
  },

  // 检查有没有token
  checkToken() {
    const token = wx.getStorageSync('token');
    const newName = `HI,` + wx.getStorageSync("nickName")
    if (token) {
      this.setData({
        isLogin: true,
        nickName: newName
      })
      console.log("token", token);
      console.log("isLogin", this.data.isLogin);
      this.getUserInfo()
    } else {
      this.setData({
        isLogin: false
      })
      console.log("token=====", token);
      console.log("isLogin=======", this.data.isLogin);
    }
  },


  async getUserInfo() {
    try {
      const res = await request({
        url: indexApi.getCurUserInfo(),
        data: {}
      });
      console.log("getUserInfo", res);
      const {
        avatar,
        nickName,
        serviceProviderStatus,
        total
      } = res.data
      this.setData({
        avatar: avatar || "https://zntg.swehszn.com/file/WechatIMG640.png",
        nickName: nickName || "用户名",
        total: total || 0
      })
      wx.setStorageSync('serviceProviderStatus', serviceProviderStatus);
    } catch (error) {
      console.error("Error:", error);
    }
  },

  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === 'function') {
        this.getTabBar((tabBar) => {
          tabBar.setData({
            selected: 4
          })
        })
      }
    }
  },

  //我的地址
  myAddress() {
    wx.navigateTo({
      url: '/packageUserCenter/pages/address/index',
    })
  },

  //我的商品
  toMyProduct(){
    wx.navigateTo({
      url: '/packageUserCenter/pages/myProducts/index',
    })
  },

  // 订单售后
  orderAfterSales(){
    wx.navigateTo({
      url: '/packageUserCenter/pages/orderSales/index',
    })
  },

  // 申请成为师傅
  fillInformation() {
    wx.navigateTo({
      url: '/packageUserCenter/pages/worker/index',
    })
  },
  //个人资料
  toPerson() {
    wx.navigateTo({
      url: `/packageUserCenter/pages/person/index?avatar=${this.data.avatar}&nickName=${this.data.nickName}`,
    })
  },

  // 去登录
  toLogin() {
    wx.navigateTo({
      url: '/pages/login/index',
    })
  },


  outLog() {
    Dialog.confirm({
      title: '亲，您确定要退出登录吗',
    }).then(() => {
      const app = getApp();
      app.updateLoginStatus(false);
      wx.removeStorageSync('token');
      this.setData({
        isLogin: false,
        avatar: "https://zntg.swehszn.com/file/WechatIMG640.png",
        nickName: "用户名",
        total: 0,
        withdraw: 0,
        coupon: 0,
      });

      wx.switchTab({
        url: '/pages/home/index',
      });
    }).catch(() => {
      // 用户取消退出登录的操作
    });
  },
  //去模板
  toMyTemplate() {
    wx.navigateTo({
      url: '/packageUserCenter/pages/myTemplate/index',
    })
  },

  //按键面板
  toKeyPanel(){
    wx.navigateTo({
      url: '/packageUserCenter/pages/keyPanel/index',
    })
  },
  
  //帮助中心
  helpCenter() {
    wx.navigateTo({
      url: '/packageUserCenter/pages/helpCenter/index',
    })
  },

  //去充值
  toRecharge() {
    wx.navigateTo({
      url: '/packageUserCenter/pages/recharge/index',
    })
  },

  // 等待开发
  developmentWaiting(){
    Toast('待开发');
  },

  // 分享给朋友
  onShareAppMessage: function () {
    return {
      title: '方案报价',
      path: '/pages/usercenter/index',
    };
  },

  // 分享到朋友圈
  onShareTimeline: function () {
    return {
      title: '方案报价',
      query: ''
    };
  }
});